package model;

import java.awt.Dimension;

import model.semantics.Operation;


/**
 * Evaluate an expression for each pixel in a image.
 * 
 * @author Robert C Duvall
 * @author Troy Ferrell(modified)
 */
public class Model
{
    public static final double DOMAIN_MIN = -1;
    public static final double DOMAIN_MAX = 1;
    
    private Parser myParser;

    // TODO; hold map of variables
    // TODO: hold list of expressions
    
    /**
     * Create a basic model of the system.
     */
    public Model ()
    {
        myParser = new Parser();
    }

    /**
     * Evaluate an expression for each point in the image.
     */
    public Pixmap evaluate (String infix, Dimension size)
    {
    	Operation toEval = createExpression(infix);
        Pixmap result = new Pixmap(size);
        // create expression to evaluate just once
       // Expression toEval = myParser.makeExpression(infix);
        // evaluate it for each pixel
        for (int imageY = 0; imageY < size.height; imageY++)
        {
            double evalY = imageToDomainScale(imageY, size.height);
            RGBColor colorY = new RGBColor(evalY);
            for (int imageX = 0; imageX < size.width; imageX++)
            {
                double evalX = imageToDomainScale(imageX, size.width);
                RGBColor colorX = new RGBColor(evalX);
               result.setColor(imageX, imageY, toEval.evaluate(colorX, colorY).toJavaColor());
            }
        }
        return result;
    }
    

    /**
     * Convert from image space to domain space.
     */
    protected double imageToDomainScale (int value, int bounds)
    {
        double range = DOMAIN_MAX - DOMAIN_MIN;
        return ((double)value / bounds) * range + DOMAIN_MIN;
    }
    
    /**
     * A place holder for more interesting way to build the expression.
     */
    private Operation createExpression (String expression)
    {
         return myParser.makeExpression("");
    }
}
